<?php
# http://projects:8081/cdapp-2kt-api/api/seat_atis_refresh.php
header('Content-type: application/json');
include_once './_portal.php';
include_once './request_sipds.php';

// const MIN_TTL = 10 * 1000; # unit: ms
// const MAX_TTL = 30 * 1000; # unit: ms

define('MIN_TTL', init_param('SIPDS_ATIS_TTL_MIN', 10000));
// default 10000 (ms)
define('MAX_TTL', init_param('SIPDS_ATIS_TTL_MAX', 30000));
// default 30000 (ms)

const PARAM_NAME = 'atisRefreshTime';
const API_NAME   = 'basicInfoWebService.findAtis';

$refresh_info   = get_param(PARAM_NAME);
$request_result = null;
if ($refresh_info) {
  $ttl_verify = verify_ttl($refresh_info['tx'], $refresh_info['rx'], MIN_TTL, MAX_TTL);
  $info       = 'seat_atis_refresh: ' . $ttl_verify['info'];
  $status     = isset($ttl_verify['status']) ? $ttl_verify['status'] : 0;
  $time       = $ttl_verify['time'];

  if ($status > 0) {
    save_param(PARAM_NAME, ['tx' => $time, 'rx' => $refresh_info['rx']]);
    $request_result = refresh_data(API_NAME);
    $info .= ' ' . $request_result['info'];
    $status  = $status * $request_result['status'];
    $time_rx = timestamp();
    save_param(PARAM_NAME, ['tx' => $time, 'rx' => $time_rx]);
  }

  output([
    'status'  => $status,
    'info'    => $info,
    'content' => exec_sql("SELECT * FROM `2kt_seat_atis`"),
    'request' => $request_result,
  ]);
} else {
  output([
    'status'  => -1,
    'info'    => 'missing refresh_info!',
    'content' => [],
    'request' => $request_result,
  ]);
}

die();

# request content from sipds
function refresh_data($api_name) {
  $response = request_sipds($api_name);
  if ($response['status'] == 0) {
    return [
      'status' => 0,
      'info'   => 'Failed to refresh data, sending request failed.',
    ];
  }
  $sql_array     = [];
  $result        = true;
  $response_data = $response['data'];
  foreach ($response_data as $atis) {
    $fullContent = json_stringify($atis);
    // $sql = "UPDATE `2kt_seat_atis` SET `arrivalCode`='{$atis['ARRVERSION']}',`arrivalVersion`='{$atis['ARRUPDATETIME']}',`departureCode`='{$atis['DEPVERSION']}',`departureVersion`='{$atis['DEPUPDATETIME']}',`autoMode`={$atis['ISAUTOSYNC']},`fullContent`='{$fullContent}' WHERE `airportCode`='{$atis['ICAO']}'";
    $sql = "INSERT INTO `2kt_seat_atis`(`airportCode`, `arrivalCode`, `arrivalVersion`, `departureCode`, `departureVersion`, `autoMode`, `fullContent`, `color`, `defaultColor`) VALUES ('{$atis['ICAO']}','{$atis['ARRVERSION']}','{$atis['ARRUPDATETIME']}','{$atis['DEPVERSION']}','{$atis['DEPUPDATETIME']}','{$atis['ISAUTOSYNC']}','{$fullContent}','#888888','#888888') ON DUPLICATE KEY UPDATE `arrivalCode`='{$atis['ARRVERSION']}',`arrivalVersion`='{$atis['ARRUPDATETIME']}',`departureCode`='{$atis['DEPVERSION']}',`departureVersion`='{$atis['DEPUPDATETIME']}',`autoMode`={$atis['ISAUTOSYNC']},`fullContent`='{$fullContent}'";
    array_push($sql_array, $sql);
  }
  $result_arr = exec_sql_array($sql_array);
  foreach ($result_arr as $r) {
    $result = $result && $r;
  }
  if ($result) {
    return [
      'status'   => 1,
      'info'     => 'Successed to refresh data.',
      'response' => $response,
    ];
  } else {
    return [
      'status'   => 0,
      'info'     => 'Failed refresh data, database update filed.',
      'response' => $response,
    ];
  }
}
